import { QueryClient, useMutation } from "@tanstack/react-query";
import { AxiosError } from "axios";
import { LessonService } from "@/shared/api/api.lesson";
import { ILesson } from "@/shared/interfaces";
import { RawDraftContentState } from "draft-js";

export function useSaveLesson() {
    const queryClient = new QueryClient

    const saveLessonMut = useMutation(
        {
            mutationFn: LessonService.update,
            onError: (error: (AxiosError<{ message: string }>)) => error,
        }, queryClient
    );
    const errorMessage = saveLessonMut.error?.response?.data.message

    return {
        errorMessage,
        isSuccess: saveLessonMut.isSuccess,
        handleSubmit: (data: { lessonId: number, content: RawDraftContentState, testIds: number[] }) => {
            saveLessonMut.mutate(
                {
                    id: data.lessonId,
                    dto: {
                        content: data.content,
                        testIds: data.testIds
                    }
                })
        }
    };
}